# -*-* encoding:UTF-8 -*-
# author            : mengy
# date              : 2019/1/28
# python-version    : Python 3.7.0
# description       : 配置解析工具

import os
import configparser


class CONFIG(object):
    """
    配置文件解析
    """
    __cur_path = os.path.dirname(__file__)
    __config_path = os.path.join(__cur_path, 'config.ini')
    __config = configparser.ConfigParser()
    __config.read(__config_path, 'utf-8')

    def get_configs(self, section):
        dicts = []
        try:
            if self.__config.has_section(section):
                options = self.__config.options(section)
                for option in options:
                    dicts.append({option: self.__config.get(section, option)})
        except Exception as e:
            print(e)
        return dicts

    def get_value(self, section, name):
        return self.__config.get(section, name)

    @staticmethod
    def config():
        return CONFIG()


if __name__ == '__main__':
    config = CONFIG()
    dicts = config.get_configs("DATASOURCE")
    print(dicts)
    print(config.get_value("DATASOURCE", "DB_NAME"))
